/*
 * Sem_scheduler.c
 *
 *  Created on: Mar 1, 2015
 *      Author: Diana
 */

#include "derivative.h" /* include peripheral declarations */


/* Macros */
#define rojo 0x00040000    // Mask for PTB18
#define verde 0x00080000   // Mask for PTB19
#define azul 0x00000002    // Mask for PTD1
#define RyV 0x000C0000     // Mask for PTB18 and PTB19

/* Functions */
void Configuracion();
void Azul_On ();
void Azul_Off ();
void Verde_On ();
void Verde_Off ();
void Rojo_On();
void Rojo_Off();






void Sem_scheduler()
{
		
		
	static	unsigned char segundos=0;  // A static variable keeps its value among calls
	
		            
		if ( segundos <= 5)  // It keeps turned on LED red during 6 seconds
		{
			Azul_Off();     // Turns off Blue LED
			Rojo_On();      // Turns on Red LED
			segundos++;     // segundos represents the number of seconds the red LED keeps turned on
		}
		   else             // Once six seconds have passed... 
		     {
			   Rojo_Off();  // Turns off red LED
			   Verde_On();  // Turns on green LED
			  segundos++;   // Counts the seconds
			     if (segundos>=12)  // It means the green LED was turned on during 5 seconds 
			     {
			    	 segundos=0;   // To begin again 
			    	 Rojo_Off();   //  Turns off red LED
			    	 Verde_Off();  // Turns off green LED 
			    	 Azul_On();    // Turns on blue LED
			     }
			   
		     }
			
		
	
	
}

void Azul_On ()
{
	                   GPIOD_PCOR |=azul;  // Turns on blue LED
					
}
void Azul_Off ()
{
	                   
						GPIOD_PSOR =azul;  // Turns off blue LED
}

void Verde_On ()
{
	                   GPIOB_PCOR |=verde;  // Turns on green LED
					  
}

void Verde_Off ()
{
	                   
						GPIOB_PSOR =verde;  // Turns off green LED
}

void Rojo_On ()
{
	                   GPIOB_PCOR |=rojo;   // Turns on red LED
					  
}
void Rojo_Off ()
{
	                  
						GPIOB_PSOR =rojo;   // Turns off red LED
}

void Configuracion ()
{
	        SIM_SCGC5 = 0x00001400;  // Clock port B y D
			PORTB_PCR18 = PORTB_PCR19 = PORTD_PCR1=0x00000140;  //  GPIO mode
			GPIOB_PDDR |=RyV;  // Set pins PTB18 and PTB19 as output
			GPIOD_PDDR |=azul;   // Set pin PTD1 as output
			GPIOB_PSOR =RyV; // Turns off red and green LEDs
			GPIOD_PSOR =azul;	// Turns off blue LED	
}

